﻿using Bilibili.Model.Comment;
using Bilibili.Model.Common;
using Bilibili.Model.Resource;
using Newtonsoft.Json.Linq;
using PCLUntils.Json;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class CommentClient : ClientBase
    {
        public async Task<ReturnModel<DataComment>> GetChild(string oId, int emoteType, long rpid, int loadpage)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo(false, "https://api.bilibili.com/x/v2/reply/main", $"&oid={oId}&plat=2&pn={loadpage}&ps=10&root={rpid}&type={emoteType}");
                var re = await ApiClient.GetResults(apiInfo);
                DataComment m = re.ParseObject<DataComment>();
                return BuildSuccessResult(m);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<DataComment>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> Send(CommentData data, string text)
        {
            try
            {
                string content = string.Empty;
                switch (data.Type)
                {
                    case CommentPostType.New:
                        {
                            DataInfo dataInfo = new DataInfo($"&type={data.EmoteType}&oid={data.Oid}&message={Uri.EscapeDataString(text)}");
                            content = dataInfo.ToString();
                            break;
                        }
                    case CommentPostType.ReplyAt:
                        {
                            text = "回复 @" + data.UserName + ":" + text;
                            DataInfo dataInfo = new DataInfo($"&type={data.EmoteType}&oid={data.Oid}&message={Uri.EscapeDataString(text)}&root={data.Root}&parent={data.RpId}");
                            content = dataInfo.ToString();
                            break;
                        }
                    case CommentPostType.ReplyComment:
                        {
                            DataInfo dataInfo = new DataInfo($"&type={data.EmoteType}&oid={data.Oid}&message={Uri.EscapeDataString(text)}&root={data.RpId}&parent={data.RpId}");
                            content = dataInfo.ToString();
                            break;
                        }
                }
                var re = await ApiClient.PostResults("https://api.bilibili.com/x/v2/reply/add", content);
                var obj = re.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> Delete(int commentType, string aId, long rpid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"&type={commentType}&oid={aId}&rpid={rpid}");
                var re = await ApiClient.PostResults("https://api.bilibili.com/x/v2/reply/del", dataInfo.ToString());
                var obj = re.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<DataComment>> Get(int commentType, string aId, long currentPage, int sort)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo(false, "https://api.bilibili.com/x/v2/reply/main", $"&oid={aId}&plat=2&pn={currentPage}&ps=20&sort={sort}&type={commentType}");
                var result = await ApiClient.GetResults(apiInfo);
                DataComment model = result.ParseObject<DataComment>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<DataComment>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> Like(int commentType, string aid, long rpid, int action)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"&type={commentType}&rpid={rpid}&oid={aid}&action={(action == 0 ? 1 : 0)}");
                var re = await ApiClient.PostResults("https://api.bilibili.com/x/v2/reply/action", dataInfo.ToString());
                var obj = re.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
    }
}